<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="总金额" prop="sumMoney">
      <el-input v-model="dataForm.sumMoney" placeholder="总金额"></el-input>
    </el-form-item>
    <el-form-item label="实付金额" prop="payMoney">
      <el-input v-model="dataForm.payMoney" placeholder="实付金额"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="orderTime">
      <el-input v-model="dataForm.orderTime" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodId">
      <el-input v-model="dataForm.goodId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="订单状态 1、处理中  2、待处理" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态 1、处理中  2、待处理"></el-input>
    </el-form-item>
    <el-form-item label="支付状态 1、待支付  2、已支付" prop="payStatus">
      <el-input v-model="dataForm.payStatus" placeholder="支付状态 1、待支付  2、已支付"></el-input>
    </el-form-item>
    <el-form-item label="发货状态 1、代发货  2、已发货" prop="sendStatus">
      <el-input v-model="dataForm.sendStatus" placeholder="发货状态 1、代发货  2、已发货"></el-input>
    </el-form-item>
    <el-form-item label="收货状态 1、收获成功  2、退货中" prop="putStatus">
      <el-input v-model="dataForm.putStatus" placeholder="收货状态 1、收获成功  2、退货中"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          orderNum: '',
          userId: '',
          sumMoney: '',
          payMoney: '',
          orderTime: '',
          goodId: '',
          orderStatus: '',
          payStatus: '',
          sendStatus: '',
          putStatus: ''
        },
        dataRule: {
          orderNum: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          sumMoney: [
            { required: true, message: '总金额不能为空', trigger: 'blur' }
          ],
          payMoney: [
            { required: true, message: '实付金额不能为空', trigger: 'blur' }
          ],
          orderTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          goodId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态 1、处理中  2、待处理不能为空', trigger: 'blur' }
          ],
          payStatus: [
            { required: true, message: '支付状态 1、待支付  2、已支付不能为空', trigger: 'blur' }
          ],
          sendStatus: [
            { required: true, message: '发货状态 1、代发货  2、已发货不能为空', trigger: 'blur' }
          ],
          putStatus: [
            { required: true, message: '收货状态 1、收获成功  2、退货中不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderNum = data.order.orderNum
                this.dataForm.userId = data.order.userId
                this.dataForm.sumMoney = data.order.sumMoney
                this.dataForm.payMoney = data.order.payMoney
                this.dataForm.orderTime = data.order.orderTime
                this.dataForm.goodId = data.order.goodId
                this.dataForm.orderStatus = data.order.orderStatus
                this.dataForm.payStatus = data.order.payStatus
                this.dataForm.sendStatus = data.order.sendStatus
                this.dataForm.putStatus = data.order.putStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'orderNum': this.dataForm.orderNum,
                'userId': this.dataForm.userId,
                'sumMoney': this.dataForm.sumMoney,
                'payMoney': this.dataForm.payMoney,
                'orderTime': this.dataForm.orderTime,
                'goodId': this.dataForm.goodId,
                'orderStatus': this.dataForm.orderStatus,
                'payStatus': this.dataForm.payStatus,
                'sendStatus': this.dataForm.sendStatus,
                'putStatus': this.dataForm.putStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
